home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Utils / IE Tab / Bin / ie_tab-1.0.8-fx+fl+mz.xpi / components / nsIeTabWatchFactory.js next >
Text File  |  2005-12-06  |  6KB  |  140 lines

  1. /*
  2.  * Copyright (c) 2005 yuoo2k <yuoo2k@gmail.com>
  3.  *
  4.  * This library is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU Lesser General Public
  6.  * License as published by the Free Software Foundation; either
  7.  * version 2.1 of the License, or (at your option) any later version.
  8.  *
  9.  * This library is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12.  * Lesser General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU Lesser General Public
  15.  * License along with this library; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  *
  18.  */
  19.  
  20. const _IETABWATCH_CID = Components.ID('{3fdaa104-5988-4050-94fc-c711d568fe64}');
  21. const _IETABWATCH_CONTRACTID = "@mozilla.org/ietabwatch;1";
  22.  
  23. // IeTabWatcher object
  24. var IeTabWatcher = {
  25.    getIeTabURL: function(url) {
  26.       return "chrome://ietab/content/reloaded.html?url=" + url.replace(/\.\./g,"\\\\");
  27.    },
  28.  
  29.    getCharPref: function(prefName, defval) {
  30.       var result = defval;
  31.       var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  32.       var prefs = prefservice.getBranch("");
  33.       return (prefs.getPrefType(prefName) == prefs.PREF_STRING ? prefs.getCharPref(prefName) : result);
  34.    },
  35.  
  36.    getBoolPref: function(prefName, defval) {
  37.       var result = defval;
  38.       var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  39.       var prefs = prefservice.getBranch("");
  40.       return (prefs.getPrefType(prefName) == prefs.PREF_BOOL ? prefs.getBoolPref(prefName) : result);
  41.    },
  42.  
  43.    isFilterEnabled: function() {
  44.       return (this.getBoolPref("ietab.filter", true));
  45.    },
  46.  
  47.    getPrefFilterList: function() {
  48.       var s = this.getCharPref("ietab.filterlist", null);
  49.       return (s ? s.split(" ") : "");
  50.    },
  51.  
  52.    isMatchURL: function(url, pattern) {
  53.       if ((!pattern) || (pattern.length==0)) return(false);
  54.       var     repat = pattern;
  55.       repat = repat.replace(/\\/, "\\\\");
  56.       repat = repat.replace(/\./g, "\\.");
  57.       repat = repat.replace(/\?/g, "\\?");
  58.       repat = repat.replace(/\//g, "\\/");
  59.       repat = repat.replace(/\*/g, ".*");
  60.       repat = repat.replace(/\.\*\\\./g, ".*\\.?\\b");
  61.       repat = "^" + repat;
  62.       var     reg = new RegExp(repat);
  63.       var     matched = (reg.test(url));
  64.       return(matched);
  65.    },
  66.  
  67.    isMatchFilterList: function(url) {
  68.       var aList = this.getPrefFilterList();
  69.       for (var i=0; i<aList.length; i++) {
  70.          if (this.isMatchURL(url, aList[i])) return(true);
  71.       }
  72.       return(false);
  73.    }
  74. }
  75.  
  76. // ContentPolicy class
  77. var IeTabWatchFactoryClass = {
  78.   // nsIContentPolicy interface implementation
  79.   shouldLoad: function(contentType, contentLocation, requestOrigin, requestingNode, mimeTypeGuess, extra) {
  80.     if (contentType == Components.interfaces.nsIContentPolicy.TYPE_DOCUMENT ||
  81.         contentType == Components.interfaces.nsIContentPolicy.DOCUMENT) {
  82.       // check IeTab FilterList
  83.       if (IeTabWatcher.isFilterEnabled() && IeTabWatcher.isMatchFilterList(contentLocation.spec)) {
  84.         contentLocation.spec = IeTabWatcher.getIeTabURL(contentLocation.spec); // load in IETab
  85.       }
  86.     }
  87.     return (Components.interfaces.nsIContentPolicy.ACCEPT ?
  88.             Components.interfaces.nsIContentPolicy.ACCEPT : true);
  89.   },
  90.   // this is now for urls that directly load media, and meta-refreshes (before activation)
  91.   shouldProcess: function(contentType, contentLocation, requestOrigin, requestingNode, mimeType, extra) {
  92.     return (Components.interfaces.nsIContentPolicy.ACCEPT ?
  93.             Components.interfaces.nsIContentPolicy.ACCEPT : true);
  94.   },
  95. }
  96.  
  97. // Factory object
  98. var IeTabWatchFactoryFactory = {
  99.   createInstance: function(outer, iid) {
  100.     if (outer != null) throw Components.results.NS_ERROR_NO_AGGREGATION;
  101.     return IeTabWatchFactoryClass;
  102.   },
  103. }
  104.  
  105. // Module object
  106. var IeTabWatchFactoryModule = {
  107.   registerSelf: function(compMgr, fileSpec, location, type) {
  108.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  109.     compMgr.registerFactoryLocation(_IETABWATCH_CID, "IETab content policy", _IETABWATCH_CONTRACTID, fileSpec, location, type);
  110.     var catman = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
  111.     catman.addCategoryEntry("content-policy", _IETABWATCH_CONTRACTID, _IETABWATCH_CONTRACTID, true, true);
  112.   },
  113.  
  114.   unregisterSelf: function(compMgr, fileSpec, location) {
  115.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  116.     compMgr.unregisterFactoryLocation(_IETABWATCH_CID, fileSpec);
  117.     var catman = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
  118.     catman.deleteCategoryEntry("content-policy", _IETABWATCH_CONTRACTID, true);
  119.   },
  120.  
  121.   getClassObject: function(compMgr, cid, iid) {
  122.     if (!cid.equals(_IETABWATCH_CID))
  123.       throw Components.results.NS_ERROR_NO_INTERFACE;
  124.  
  125.     if (!iid.equals(Components.interfaces.nsIFactory))
  126.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  127.  
  128.     return IeTabWatchFactoryFactory;
  129.   },
  130.  
  131.   canUnload: function(compMgr) {
  132.     return true;
  133.   }
  134. };
  135.  
  136. // module initialisation
  137. function NSGetModule(comMgr, fileSpec) {
  138.   return IeTabWatchFactoryModule;
  139. }
  140.